#ifndef WORLDSERVER_H
#define WORLDSERVER_H

#include <map>
#include "NetWork/NetDef.h"
#include "mysql++.h"
#include "DB/AsynDBopt.h"
#include "Logic/LogicTypeDef.h"

class LoginPlayerlist;
class GameingPlayerlist;

class NetWork;
class DBConnection;


class WorldServer
{
public:
	enum
	{
		LoginServerID = 1,
		MaxtimesPerFrame = 100,
	};
	WorldServer();
	~WorldServer();
	void init();
	void clean();
	void run();
public:
	void							  onNewPlayer(Server_Con,ConectionID);
	void							  onDisconnect(ConectionID,bool);
	NetObject_PTR					  getNetObject(ConectionID id)const;

	LoginPlayer_PTR					  getLogginPlayer(ConectionID id)const;
	void				              addGamePlayer(Player_PTR player);
	Player_PTR			              getGamePlayer(ConectionID id)const;
	void				              addFinishedDBopt(AsynDB_CallBack* pCallback);
	DBConnection*		              getDBConnection()const;
	bool				              kickPlayerIFonline(const std::string& name);
private:
	LoginPlayerlist*                  m_loginPlayerlist;
	GameingPlayerlist*                m_gameingPlyaerlist;
	NetWork*					   	  m_net;
	bool                              m_stop;

	std::map<ConectionID,NetObject_PTR> m_players;
	mutable My_CriticalSection        m_playerCritical;
	DBConnection*			          m_DBconnection;
	My_CriticalSection                m_DBFinishedCritical;
	std::vector<AsynDB_CallBack*>     m_DBOptFinished;
private:
	void processDBCallBack();
};

extern WorldServer* g_WorldServer;
#endif
